﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Newtonsoft.Json.Linq;
using TaggerApp;


namespace TaggerApp
{
    /// <summary>
    /// Summary description for GetTags
    /// </summary>
    public class GetTags : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            MakeTags myTags = new MakeTags();
            context.Response.ContentType = "application/json";
            string json = new StreamReader(context.Request.InputStream).ReadToEnd();
            json = HttpUtility.UrlDecode(json);
            //JObject jsonObj = JObject.Parse(context.Request.Params["productInfo"]);
            JArray jsonObj = (JArray)JObject.Parse(json)["productInfo"];
            JArray images = new JArray();
            for (int i = 0; i < jsonObj.Count; i++)
            {
                string productName = (string)jsonObj[i]["productName"];
                string productGUID = (string)jsonObj[i]["productGUID"];
                string tag = myTags.RenderTag(productName, productGUID);
                string qr = myTags.RenderQR(productName, productGUID);
                JObject img = new JObject();
                img.Add("tag", tag);
                img.Add("qr", qr);
                img.Add("productName", productName);
                images.Add(img);
            }
            context.Response.Write(images.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}